package com.km.util;

import com.google.common.base.Joiner;
import com.km.entity.ArticleMd;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.ParserEmulationProfile;
import com.vladsch.flexmark.util.options.MutableDataSet;
import org.apache.commons.lang3.StringUtils;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/10/14.
 */
public class ArticleUtil {

    public static void splitWord(ArticleMd articleMd, String text) throws IOException {
        String[] str = text.split("-");
        String title = str[1].substring(0, str[1].length() - 3);
        StringReader sr = new StringReader(title);
        IKSegmenter ik = new IKSegmenter(sr, true);
        Lexeme lex = null;
        StringBuilder sb = new StringBuilder();
        while ((lex = ik.next()) != null) {
            sb.append(lex.getLexemeText() + ",");
        }
        articleMd.setTitle(title);
        articleMd.setKeywords(sb.toString().substring(0, sb.toString().length() - 1));

    }

    public static void mdToHtml(ArticleMd articleMd, String filePath) throws UnsupportedEncodingException, FileNotFoundException {
        // 从文件中读取markdown内容
//        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("test.md");
        InputStream stream = new FileInputStream(new File(filePath));
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "utf-8"));

        List<String> list = reader.lines().collect(Collectors.toList());

        String content = null;

        if (list.size() > 0) {
            String firstStr = list.get(0);
            if (!StringUtils.isEmpty(firstStr) && firstStr.contains("tags:")) {
                articleMd.setTagsList(Arrays.asList(firstStr.split(":")[1].trim().split(",")));
                content = Joiner.on("\n").join(list.subList(1, list.size()));
            } else {
                content = Joiner.on("\n").join(list);
            }
        }
        MutableDataSet options = new MutableDataSet();
        options.setFrom(ParserEmulationProfile.MARKDOWN);
        options.set(Parser.EXTENSIONS, Collections.singletonList(TablesExtension.create()));
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();

        Node document = parser.parse(content);
        String html = renderer.render(document);
        String[] contents = html.split("\\n");
        for (int i = 2; i < 10; i++) {
            String[] desc = new String[i];
            System.arraycopy(contents, 0, desc, 0, i);
            String description = Arrays.asList(desc).toString();
            if (description.contains("</code>")) {
                if (contents.length > 10) {
                    articleMd.setDescription(Joiner.on("\n").join(Arrays.asList(desc)));
                } else {
                    articleMd.setDescription(html);
                }
                break;
            } else if (!description.contains("<code>") & !description.contains("</code>")) {
                if (contents.length > 10) {
                    articleMd.setDescription(Joiner.on("\n").join(Arrays.asList(desc)));
                } else {
                    articleMd.setDescription(html);
                }
                break;
            }
            if (i == 9){
                if (contents.length > 10) {
                    articleMd.setDescription(html);
                } else {
                    articleMd.setDescription(html);
                }
                break;
            }
        }


        String[] desc = new String[3];
        System.arraycopy(contents, 0, desc, 0, 3);


        articleMd.setContent(html);
        articleMd.setOriginal(content);
    }

}
